package com.sqfw.project.wp.controller;

import com.sqfw.common.utils.StringUtils;
import com.sqfw.framework.aspectj.lang.annotation.Log;
import com.sqfw.framework.aspectj.lang.enums.BusinessType;
import com.sqfw.framework.web.controller.BaseController;
import com.sqfw.framework.web.domain.AjaxResult;
import com.sqfw.framework.web.page.TableDataInfo;
import com.sqfw.project.system.domain.SysUser;
import com.sqfw.project.system.service.ISysUserService;
import com.sqfw.project.wp.domain.WpInfo;
import com.sqfw.project.wp.service.IWpInfoService;
import com.sqfw.project.wpReport.domain.WpInfoDetail;
import com.sqfw.project.wpReport.service.ZJManageService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "管理员工单管理controller")
@RestController
@RequestMapping("/wp/manageWp")
public class WpManageController extends BaseController {

    @Autowired
    private IWpInfoService wpInfoService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ZJManageService zjManageService;

    /**
     * 管理员获取工单管理全部工单列表
     *
     * @param wpInfo
     * @return
     * @throws ParseException
     */
    @PreAuthorize("@ss.hasPermi('wp:manageWp')")
    @GetMapping("/getManageWpList")
    public TableDataInfo getManageWpList(WpInfo wpInfo) throws ParseException {
        if (StringUtils.isNotEmpty(wpInfo.getInitiatorName())){
            String nickName = wpInfo.getInitiatorName();
            SysUser sysUser = new SysUser();
            sysUser.setNickName(nickName);
            List<SysUser> sysUsers = sysUserService.selectUserList(sysUser);
            List<String> collect = sysUsers.stream().map(SysUser::getCode).collect(Collectors.toList());
            wpInfo.setCodes(collect);

        }
        startPage();
        List<WpInfo> list = wpInfoService.selectWpInfoList(wpInfo);
        return getDataTable(list);
    }



    /**
     * 逻辑删除工单
     * @param wpInfo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('wp:manageWp')")
    @Log(title = "逻辑删除工单", businessType = BusinessType.DELETE)
    @PostMapping("/updateDelFlag")
    public AjaxResult updateDelFlag(@RequestBody WpInfo wpInfo) {

        return toAjax(wpInfoService.updateDelFlag(wpInfo));


    }

    /**
     * 全部工单页面列表
     * @param wpInfo
     * @return
     * @throws ParseException
     */
    @PreAuthorize("@ss.hasAnyPermi('wp:manageWp,getManageWpListAll')")
    @GetMapping("/getManageWpListAll")
    public TableDataInfo getManageWpListAll(WpInfo wpInfo) throws ParseException {

        if (StringUtils.isNotEmpty(wpInfo.getNickName())){
            String nickName = wpInfo.getNickName();
            SysUser sysUser = new SysUser();
            sysUser.setNickName(nickName);
            List<SysUser> sysUsers = sysUserService.selectUserList(sysUser);
            if (!StringUtils.isNotEmpty(sysUsers)){
                List<WpInfoDetail> wpInfoDetails = new ArrayList<>();
                return getDataTable(wpInfoDetails);
            }
            List<String> collect = sysUsers.stream().map(SysUser::getCode).collect(Collectors.toList());
            wpInfo.setCodes(collect);

        }
        startPage();

        TableDataInfo dataTable = wpInfoService.getManageWpListAll(wpInfo);

        return dataTable;
    }

    /**
     * 全部工单页面列表
     * @param wpInfo
     * @return
     * @throws ParseException
     */
    @PreAuthorize("@ss.hasPermi('wp:manageWp')")
    @GetMapping("/getZJManageWpListAll")
    public TableDataInfo getZJManageWpListAll(WpInfo wpInfo) throws ParseException {

        if (StringUtils.isNotEmpty(wpInfo.getNickName())){
            String nickName = wpInfo.getNickName();
            SysUser sysUser = new SysUser();
            sysUser.setNickName(nickName);
            List<SysUser> sysUsers = sysUserService.selectUserList(sysUser);
            if (!StringUtils.isNotEmpty(sysUsers)){
                List<WpInfoDetail> wpInfoDetails = new ArrayList<>();
                return getDataTable(wpInfoDetails);
            }
            List<String> collect = sysUsers.stream().map(SysUser::getCode).collect(Collectors.toList());
            wpInfo.setCodes(collect);

        }
        startPage();

        TableDataInfo dataTable = zjManageService.getZJManageWpListAll(wpInfo);

        return dataTable;
    }


    /**
     * 全部工单页面列表
     * @param wpInfo
     * @return
     * @throws ParseException
     */
    @PreAuthorize("@ss.hasAnyPermi('manageWp,selectManageWpListAll')")
    @GetMapping("/selectManageWpListAll")
    public TableDataInfo selectManageWpListAll(WpInfo wpInfo) throws ParseException {

        startPage();

        TableDataInfo dataTable = wpInfoService.selectManageWpListAll(wpInfo);

        return dataTable;
    }


}
